#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug  freopen("in.txt","r",stdin),freopen("out.txt","w",stdout);
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 2e5+10;
using namespace std;

int N;
ll h[maxn],w[maxn],tr[maxn],f[maxn];

int lowbit(int x){
    return x&-x;
}
void add(int x,ll v){
    for(int i = x;i<=N;i += lowbit(i)) tr[i] = max(tr[i],v);
}
ll query(int r){
    ll res = 0;
    for(int i = r;i>=1;i -= lowbit(i)) res = max(res,tr[i]);
    return res;
}

ll solve(){
    for(int i = 1;i<=N;i++){
        f[i] = query(h[i]-1) + w[i];
        add(h[i],f[i]);
    }
    ll mx = 0;
    for(int i = 1;i<=N;i++) mx = max(mx,f[i]);
    return mx;
}
int main(){
    // debug;
    ios;

    cin>>N;
    for(int i = 1;i<=N;i++) cin>>h[i];
    for(int i = 1;i<=N;i++) cin>>w[i];
    cout<<solve()<<'\n';
    return 0;
}
